/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.com.pps.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;
import org.springframework.jdbc.core.PreparedStatementCreatorFactory;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.SqlParameter;
import vn.com.pps.entities.CoursePupilFee;

/**
 *
 * @author Dai
 */
public class CourseFeeDAO extends BaseDAO implements ICourseFeeDAO {

    @Override
    public int insert(CoursePupilFee courseFee) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public int update(CoursePupilFee courseFee) {
        String sql = "UPDATE tblcoursefees set Amount=?, PaidStatus=?,PaidDate=? WHERE ID=?";
        Object[] params = new Object[]{courseFee.getAmount(), courseFee.getPaidStatus(), courseFee.getPaidOn(), courseFee.getId()
        };
        PreparedStatementCreatorFactory psc
                = new PreparedStatementCreatorFactory(sql);
        psc.addParameter(new SqlParameter("Amount", Types.DECIMAL));
        psc.addParameter(new SqlParameter("PaidStatus", Types.VARCHAR));
        psc.addParameter(new SqlParameter("PaidDate", Types.TIMESTAMP));
        psc.addParameter(new SqlParameter("ID", Types.INTEGER));
        getJdbcTemplate().update(psc.newPreparedStatementCreator(params));
        return courseFee.getId();
    }

    @Override
    public int delete(Integer id) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public CoursePupilFee getCourseFee(Integer id) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Page<CoursePupilFee> getCourseFees(int pageNo, int pageSize, String whereClause) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public int getCount(String whereClause) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public List<CoursePupilFee> getCourseFees(int courseID, int pupilID) {
        String sqlQuery = "SELECT * FROM tblcoursefees WHERE CourseID= " + courseID + " AND PupilID=" + pupilID;
        System.out.println("sqlQuery::" + sqlQuery);
        return getJdbcTemplate().query(sqlQuery, new RowMapper() {
            @Override
            public Object mapRow(ResultSet rs, int i) throws SQLException {
                CoursePupilFee coursePupilFee = new CoursePupilFee();
                coursePupilFee.setId(rs.getInt("ID"));
                coursePupilFee.setMonth(rs.getInt("Month"));
                coursePupilFee.setCourseID(rs.getInt("CourseID"));
                coursePupilFee.setPupilID(rs.getInt("PupilID"));
                coursePupilFee.setAmount(rs.getBigDecimal("Amount"));
                coursePupilFee.setPaidOn(rs.getTimestamp("PaidDate"));
                coursePupilFee.setPaidStatus(rs.getString("PaidStatus"));
                return coursePupilFee;
            }
        });

    }

}
